<template>
  <div>
    <!-- 表单 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>

        <my-channel v-model="form.channelId"></my-channel>
        <el-form-item label="日期">
          <el-date-picker
            placeholder="选择日期"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            v-model="form.dateRange"
            value-format="yyyy-MM-dd"
          style="width: 400px"></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="hQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格 -->
    <el-card class="box-card" style="margin-top:20px">
      <div slot="header" class="clearfix">
        根据条件找到了{{total_count}}条数据
      </div>
      <el-table
        :data="articles"
        style="width: 100%">

        <el-table-column
          label="封面"
          width="180">

          <template slot-scope="scope">
            <el-image :src="scope.row.cover.images[0]">
              <div slot="placeholder" class="image-slot">
                加载中<span class="dot">...</span>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题">
        </el-table-column>
        <el-table-column
          label="状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status | fStatusType">{{scope.row.status | fStatusText}}</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="发布时间" prop="pubdate" width="180">
        </el-table-column>

        <el-table-column label="操作" width="90">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              @click="hEdit(scope.$index, scope.row)" circle></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="hDelete(scope.$index, scope.row)" circle></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :hide-on-single-page="true"
        @current-change="hPageChange"
        background
        layout="prev, pager, next"
        :page-size="10"
        :total="total_count">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import MyChannel from '@/components/myChannel.vue'

export default {
  name: 'articles',
  components: {
    MyChannel
  },
  data () {
    return {
      total_count: 0, // 数量
      page: 1, // 查询的页码
      form: {
        dateRange: [], // 数组
        status: null, // 状态
        channelId: null // 频道
      },

      articles: []
    }
  },
  created () {
    this.doQuery()
  },
  filters: {
    fStatusType (status) {
      // 策略模式 --- 设计模式（套路）
      const types = {
        0: 'info',
        1: 'success',
        2: 'normal',
        3: 'warning',
        4: 'danger'
      }
      return types[status]
    },
    fStatusText (status) {
      const texts = {
        0: '草稿',
        1: '待审核',
        2: '审核通过',
        3: '审核失败',
        4: '已删除'
      }
      return texts[status]
    }
  },
  methods: {
    // 构造查询条件
    buildParams () {
      const { status, dateRange, channelId } = this.form
      const param = {
        page: this.page,
        status
      }
      if (channelId === '') {
        param.channel_id = null
      } else {
        param.channel_id = channelId
      }
      if (dateRange) {
        param.begin_pubdate = dateRange[0]
        param.end_pubdate = dateRange[1]
      }
      return param
    },
    async doQuery () {
      const paramObj = this.buildParams()
      console.log('本次查询的条件对象是', paramObj)
      try {
        const res = await this.$http({
          url: '/mp/v1_0/articles',
          method: 'GET',
          params: paramObj
        })
        console.log(res)
        // 保存数据
        this.articles = res.data.data.results
        this.total_count = res.data.data.total_count
      } catch (err) {
        console.log(err)
      }
    },
    hQuery () {
      console.log('submit!')
      this.doQuery()
    },
    hPageChange (curPage) {
      console.log('你现在点的页码是', curPage)
      // 1. 更新页码
      this.page = curPage
      this.doQuery()
    },
    hEdit (index, row) {

    },
    hDelete (index, row) {

    }
  }
}
</script>
